<template>
  <div class="ms-list">
    <el-row class="operation">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">
          <icon style="" name="home"></icon>
        </el-breadcrumb-item>
        <el-breadcrumb-item>财务管理</el-breadcrumb-item>
        <el-breadcrumb-item>消费分析</el-breadcrumb-item>
        <el-breadcrumb-item>项目平台日消费</el-breadcrumb-item>
      </el-breadcrumb>
      <el-button-group>
        <router-link :to="{path: '/customer/item-daily-consumption-line',
                           query: {data: this.table.list, search: this.search}}">
          显示图表
        </router-link>
      </el-button-group>
    </el-row>

    <ms-search :data="search.list" @click="updateConditions"></ms-search>

    <el-table
      class="element-table"
      size="small" stripe border
      :data="newList"
      :highlight-current-row="true"
      :fit="true">
      <el-table-column prop="day" label="日期" win-width="85"></el-table-column>
      <el-table-column prop="itemTitle" label="项目名称" win-width="140"></el-table-column>
      <el-table-column prop="platformTitle" label="所属平台" min-width="100"></el-table-column>
      <el-table-column prop="costCoin" label="总消费点数 (点)" min-width="100" align="right" header-align="center"></el-table-column>
      <el-table-column prop="costMoney" label="总消费现金 (元)" min-width="100" align="right" header-align="center"></el-table-column>
      <el-table-column prop="serviceCoin" label="服务户总消费 (点)" min-width="100" align="right" header-align="center"></el-table-column>
      <el-table-column prop="saler" label="销售经理" min-width="90"></el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page.sync="table.pagination.page"
      :total="table.pagination.totalRow"
      :page-size="table.pagination.pageSize"
      :page-sizes="table.pagination.pageSizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
  import MsSearch from '@/components/common/Search.vue'
  import MsConfirmButton from '@/components/common/ConfirmButton.vue'
  import MsPlatform from '@/components/common/Platform.vue'
  import Api from '@/api/customer/item-platform-daily-consumption'
  import ResponseParse from '@/utils/response-parse'
  import { PLATFORM_LIST } from '@/utils/constants'
  import { SEARCH_CONDITION } from '@/models/customer/platform-daily-consumption'

  export default {
    components: {
      MsSearch,
      MsConfirmButton,
      MsPlatform
    },
    data () {
      return {
        search: {
          list: [],
          condition: {}
        },
        table: {
          loading: true,
          list: [],
          pagination: {}
        }
      }
    },
    created () {
      this.search.list = SEARCH_CONDITION
      this.table.list = []
      this.requestList()
    },
    computed: {
      platforms () {
        return PLATFORM_LIST
      },
      newList () {
        let platforms = {}
        PLATFORM_LIST.forEach(item => {
          platforms[item.id] = item.title
        })

        let list = []
        let $list = this.table.list
        if ($list !== undefined && $list.length > 0) {
          $list.forEach(value => {
            let data = value
            let day = value.day + ''
            data.day = day.substring(0, 4) + '-' + day.substring(4, 6) + '-' + day.substring(6, 8)

            if (value.platformId !== undefined && value.platformId !== '') {
              data.platformTitle = platforms[value.platformId]
            } else {
              data.platformTitle = '未知'
            }
            list.push(data)
          })
        }
        return list
      }
    },
    methods: {
      requestList () {
        this.table.loading = true

        const search = Object.assign({}, this.table.pagination, this.search.condition)
        Api.list({search: search}, response => {
          let status = response.status || 0
          let body = response.data || []
          if (status === 200 || status === 204) {
            this.table.list = body
            this.table.pagination = ResponseParse.pagination(response.headers)
//            if (this.table.pagination.totalPage === this.table.pagination.page) {
//              this.tbHeight = ''
//            } else {
//              this.$set(this, 'tbHeight', this.PHeigh - this.$refs.msSearch.clientHeight)
//            }
          } else {
//            this.tbHeight = ''
            this.table.list = []
            this.$message.error(body.error || '获取数据失败')
          }
          this.table.loading = false
        })
      },
      handleSizeChange (val) {
        this.table.pagination.pageSize = val
        this.requestList()
      },
      handleCurrentChange (val) {
        this.table.pagination.page = val
        this.requestList()
      },
      updateConditions (conditions) {
        this.table.pagination.page = 1
        this.search.condition = conditions
        this.requestList()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .element-table th {
    height: 48px;
  }

  .element-table td {
    height: 37px;
  }
</style>
